package com.comunity_cloud.client.view2;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import com.comunity_cloud.client.controller.MainController2;
import com.user_hosted_storage.dto.CommunityDTO;

/**
 * Component to be used as tabComponent;
 * Contains a JLabel to show the text and 
 * a JButton to close the tab it belongs to 
 */ 
public class ButtonTabComponent extends JPanel {
    /**
	 * 
	 */
	private static final long serialVersionUID = -2226136324399847703L;
	
	
	
	private CommunityDTO community;

    public ButtonTabComponent(final JTabbedPane pane,CommunityDTO communityDTO) {
    	super(new BorderLayout());
    	this.community = communityDTO;
        //unset default FlowLayout' gaps
        if (pane == null) {
            throw new NullPointerException("TabbedPane is null");
        }
        
        setOpaque(false);
        
        //make JLabel read titles from JTabbedPane
//        int i = pane.indexOfTabComponent(ButtonTabComponent.this);
        JLabel label = new JLabel() {
            /**
			 * 
			 */
			private static final long serialVersionUID = -9026642517487454484L;

			public String getText() {
                int i = pane.indexOfTabComponent(ButtonTabComponent.this);
                if (i != -1) {
                    return pane.getTitleAt(i);
                } 
                return null;
            }
        };
        
        add(label, BorderLayout.CENTER);
        //add more space between the label and the button
        label.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 5));
        //tab button
        
        JPanel panel = new JPanel();
        panel.setOpaque(false);
        
        panel.setLayout(new FlowLayout(FlowLayout.LEFT));
        JLabel upload = new JLabel(new ImageIcon(getClass().getResource("/images/upload.png")));
        upload.setPreferredSize(new Dimension(16, 16));
        panel.add(upload);
        
        upload.addMouseListener(new MouseAdapter() {
        	@Override
        	public void mouseClicked(MouseEvent e) {
        		JFileChooser fileChooser = new JFileChooser();
        		int res = fileChooser.showOpenDialog(ButtonTabComponent.this);
        		if(res == JFileChooser.APPROVE_OPTION){
        			MainController2.getInstance().filesDropped(new File[]{fileChooser.getSelectedFile()},community);
        		}
        	}
		});
        
        JLabel exit = new JLabel(new ImageIcon(getClass().getResource("/images/exit.png")));
        exit.setPreferredSize(new Dimension(16, 16));
        panel.add(exit);
        add(panel,BorderLayout.SOUTH);
        //add more space to the top of the component
        setBorder(BorderFactory.createEmptyBorder(2, 0, 0, 0));
        
    }
   
}



